package com.framework.shs.commom.util;

import java.security.Principal;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;

import com.framework.shs.a.dao.UserDao;
import com.framework.shs.a.entity.User;

public class UserUtils {

	private static UserDao dao =SpringContextHolder.getBean(UserDao.class);
	
	public static User getUser() {
		Subject subject = getSubject();
		if(subject.isAuthenticated()){
			return dao.get(User.class, (String)subject.getPrincipal());
		}
		return null;
	}
	/**
	 * 获取当前登录者对象
	 */
	public static Principal getPrincipal(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Principal principal = (Principal)subject.getPrincipal();
			if (principal != null){
				return principal;
			}
//			subject.logout();
		}catch (UnavailableSecurityManagerException e) {
			
		}catch (InvalidSessionException e){
			
		}
		return null;
	}
	
	/**
	 * 获取授权主要对象
	 */
	public static Subject getSubject(){
		return SecurityUtils.getSubject();
	}
	
}
